#include "UIMusicSelectConsoleComponent.h"

using namespace UIMusicSelectConsoleComponent;

MSCHispeed * MSCHispeed::create(Size disparea)
{
	auto hs = new MSCHispeed();
	hs->initWithColor(Color4B(0, 0, 0, 0), disparea.width, disparea.height);
	hs->disparea = disparea;
	hs->barheight = disparea.height*.6f;
	hs->createText();
	hs->createBar();
	//hs->createPointer();
	return hs;
}

void MSCHispeed::createText()
{
	auto name = Label::createWithTTF("HI-SPEED", FONTA, 32);
	UI::setNodeAnch(name, ANCH_BL);
	name->setTextColor(TEXTCOLORB);
	name->setPosition(0, barheight);
	name->setScale(disparea.height*.4f / name->getContentSize().height);
	addChild(name, 50);

	//value = Label::createWithTTF(strtool::dou2str(hsvalue), FONTA, 32);
	//UI::setNodeAnch(value, ANCH_BR);
	//value->setPosition(disparea.width, barheight);
	//addChild(value, 50);
}

void MSCHispeed::createBar()
{
	bar = UICSliderBar::create(Size(disparea.width, barheight), THEMECOLORBA(0), TEXTCOLORB, 0.25f, 5.f, .05f, 2.5f, 2);
	addChild(bar);
}

void MSCHispeed::onMouseUp(Event * e)
{
	hsvalue = bar->getValue();
}


MusicSelectConsole * MusicSelectConsole::create(Size disparea, int comcols, int comrows)
{
	auto cons = new MusicSelectConsole();
	cons->initWithColor(THEMECOLORBA(230), THEMECOLORBA(200), Vec2(1, 1));
	cons->changeWidthAndHeight(disparea.width, disparea.height);
	cons->disparea = disparea;
	cons->comcols = comcols;
	cons->comrows = comrows;
	cons->comheight = disparea.height*.9f / comrows;
	cons->comwidth = disparea.width*0.95f / comcols;
	cons->comsize = Size(cons->comwidth, cons->comheight);
	cons->uicsize = Size(cons->comwidth*.9f, cons->comheight*.55f);

	UI::setNodeAnch(cons, ANCH_BL);
	auto line = LayerGradient::create(TEXTCOLORB, TEXTCOLORB, Vec2(1, 0));
	line->changeWidthAndHeight(disparea.width, disparea.height*.02f);
	line->setPositionY(disparea.height*.98f);
	cons->addChild(line);

	cons->createOptions();

	return cons;
}

Size MusicSelectConsole::getSize()
{
	return disparea;
}

void MusicSelectConsole::createTitle()
{
	auto pos = Vec2(disparea.width*0.95, disparea.height*0.92);
	auto title = Label::createWithTTF("CONSOLE", FONTA, 48);
	UI::setNodeAnch(title, ANCH_BR);
	title->setOpacity(192);
	title->setPosition(pos);
	addChild(title);

	auto line = LayerGradient::create(pUI->themeColorB(64), pUI->themeColorB(192), Vec2(1, 0));
	line->changeWidthAndHeight(disparea.width, 4);
	line->setPosition(0, pos.y);
	addChild(line);
}

void MusicSelectConsole::createOptions()
{
	auto hispeed = optionHispeed(uicsize);
	addOption(1, 1, "HI-SPEED", hispeed);

	auto shutter = UICSliderBar::create(uicsize, THEMECOLORB, TEXTCOLORB, 0, 100, 1, 0, 0);
	addOption(2, 1, "SHUTTER(Invalid)", shutter);

	auto lifter = UICSliderBar::create(uicsize, THEMECOLORB, TEXTCOLORB, 0, 50, 1, 0, 0);
	addOption(3, 1, "LIFTER(Invalid)", lifter);

	addOption(4, 1, "GAME STYLE", optionKeysetType(uicsize));

	auto trackeffect = UICDropdownList::create(uicsize, THEMECOLORB, TEXTCOLORB, TextHAlignment::LEFT);
	trackeffect->addItem("NORMAL", 0);
	trackeffect->addItem("TRACK RANDOM", 1);
	trackeffect->addItem("FULL RANDOM", 2);
	addOption(1, 2, "TRACK EFFECT(Invalid)", trackeffect);

	auto noteeffect = UICDropdownList::create(uicsize, THEMECOLORB, TEXTCOLORB, TextHAlignment::LEFT);
	noteeffect->addItem("NORMAL", 0);
	noteeffect->addItem("FADE IN", 1);
	noteeffect->addItem("FADE OUT", 2);
	noteeffect->addItem("BLINK", 3);
	addOption(2, 2, "NOTE EFFECT(Invalid)", noteeffect);

	auto speedeffect = UICDropdownList::create(uicsize, THEMECOLORB, TEXTCOLORB, TextHAlignment::LEFT);
	speedeffect->addItem("NORMAL", 0);
	speedeffect->addItem("COMBO UP", 1);
	speedeffect->addItem("COMBO DOWN", 2);
	speedeffect->addItem("WAVE", 3);
	speedeffect->addItem("RANDOM", 4);
	addOption(3, 2, "HI-SPEED EFFECT(Invalid)", speedeffect);

	auto clearmode = UICDropdownList::create(uicsize, THEMECOLORB, TEXTCOLORB, TextHAlignment::LEFT);
	clearmode->addItem("STANDARD", 0);
	clearmode->addItem("GROOVE GAUGE", 1);
	clearmode->addItem("HEALTH GAUGE", 2);
	clearmode->addItem("SUDDEN DEATH", 3);
	addOption(4, 2, "CLEAR MODE(Invalid)", clearmode);
}

void MusicSelectConsole::addOption(int col, int row, string name, Node * uicomponent)
{
	auto pos = compos(col, row);
	auto text = Label::createWithTTF(name, FONTA, 32);
	UI::setNodeAnch(text, ANCH_BL);
	text->setTextColor(TEXTCOLORB);
	text->setPosition(pos.x, pos.y + comheight*.6f);
	text->setScale(comheight*.4f / text->getContentSize().height);
	addChild(text);
	uicomponent->setPosition(pos.x, pos.y + .05f*comheight);
	uicomponent->setName(name);
	addChild(uicomponent);
}

UICSliderBar * UIMusicSelectConsoleComponent::optionHispeed(Size size)
{
	auto hispeed = UICSliderBar::create(size, THEMECOLORB, TEXTCOLORB, .5f, 5.0f, .05f, pConfig->getFloatValue("gameplay", "hispeed"), 2);
	hispeed->setOnValueChangeAction([=]
	{
		pConfig->setFloatValue("gameplay", "hispeed", hispeed->getValue(), "value", false);
	});
	hispeed->setAdditionalEventAction(EventHelper::MOUSEUP, [=]
	{
		pConfig->saveConfig();
	}
	);
	return hispeed;
}

UICDropdownList * UIMusicSelectConsoleComponent::optionKeysetType(Size size)
{
	auto keyset = UICDropdownList::create(size, THEMECOLORB, TEXTCOLORB, TextHAlignment::LEFT);
	keyset->addItem("AUTO", 0);
	keyset->addItem("NORMAL STYLE", 1);
	keyset->addItem("BM STYLE", 2);
	keyset->setAdditionalEventAction(EventHelper::MOUSEDOWN, [=]
	{
		pConfig->setIntValue("gameplay", "gamestyle", keyset->getSelectedTag());
	});
	keyset->setSelectedIndex(pConfig->getIntValue("gameplay", "gamestyle"));
	return keyset;
}
